// pages/index/job/list.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    filter: [
      {
        title: '地区', is_show: false, list: [
          {
            value: '全部', is_selected: true
          },
          {
            value: '庆云县', is_selected: false, sub_list: [
              { value: '全部', is_selected: true },
              { value: '渤海路街道', is_selected: false },
              { value: '庆云镇', is_selected: false },
              { value: '常家镇', is_selected: false },
              { value: '尚堂镇', is_selected: false },
              { value: '崔口镇', is_selected: false },
              { value: '东辛店镇', is_selected: false },
              { value: '严务乡', is_selected: false },
              { value: '中丁乡', is_selected: false },
              { value: '徐园子乡', is_selected: false }]
          }
        ]
      },
      {
        title: '学历', is_show: false, list: [
          { value: '全部', is_selected: true },
          { value: '不限', is_selected: false },
          { value: '小学', is_selected: false },
          { value: '初中', is_selected: false },
          { value: '高中', is_selected: false },
          { value: '大专', is_selected: false },
          { value: '本科', is_selected: false },
          { value: '硕士', is_selected: false },
          { value: '博士', is_selected: false }
        ]
      },
      {
        title: '工作经验', is_show: false, list: [
          { value: '全部', is_selected: true },
          { value: '不限', is_selected: false },
          { value: '1年以下', is_selected: false },
          { value: '1-2年', is_selected: false },
          { value: '3-5年', is_selected: false },
          { value: '6-7年', is_selected: false },
          { value: '8-10年', is_selected: false },
          { value: '10年以上', is_selected: false }
        ]
      }
    ]
  },
  backto: function (e) {
    wx.navigateBack({})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中…',
    })
    const arr = { job: 1, qiu: 2, jianzhi: 3};
    const title = { job: '全职招聘', qiu: '求职简历', jianzhi: '兼职招聘'};
    wx.setNavigationBarTitle({
      title: title[options.type],
    })
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Index/getAddressFilter?token=' + this.data.token,
      success: res => {
        var new_filter = this.data.filter;
        new_filter[0] = res.data;
        this.setData({
          filter: filter[options.type]
        })
      }
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Job/lists?type=' + arr[options.type] + '&token=' + this.data.token,
      success: res => {
        this.setData({
          list: res.data,
          type: options.type
        })
        wx.hideLoading()
      }
    })
  },
  setFilterPanel: function (e) {
    var findex = e.currentTarget.dataset.findex;
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      if (key == findex) {
        filter[key].is_show = !value.is_show;
      } else {
        filter[key].is_show = false;
      }
    })
    this.setData({
      filter: filter
    })
  },
  hideFilter: function () {
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      filter[key].is_show = false;
    })
    this.setData({
      filter: filter
    })
  },
  setCateIndex: function (e) {
    var keys = e.currentTarget.dataset.keys;
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      if (value.is_show) {
        value.list.forEach(function (vo, idx) {
          if (idx == keys) {
            filter[key].list[idx].is_selected = true;
          } else {
            filter[key].list[idx].is_selected = false;
          }
        })
      }
    })
    this.setData({
      filter: filter
    })
  },
  setSubcateIndex: function (e) {
    console.log(e);
    var subindex = e.currentTarget.dataset.key;
    var filter = this.data.filter;
    filter.forEach(function (value, key) {
      if (value.is_show) {
        value.list.forEach(function (item, index) {
          if (item.is_selected) {
            item.sub_list.forEach(function (vo, idx) {
              if (idx == subindex) {
                filter[key].list[index].sub_list[idx].is_selected = true;
              } else {
                filter[key].list[index].sub_list[idx].is_selected = false;
              }
            })
          }
        })
      }
    })
    this.setData({
      filter: filter
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})